package application.view 
{
	import application.controller.HoverDragController;
	import away3d.containers.Scene3D;
	import away3d.containers.View3D;
	import away3d.debug.AwayStats;
	import away3d.lights.DirectionalLight;
	import away3d.lights.LightBase;
	import away3d.lights.PointLight;
	import away3d.materials.BitmapMaterial;
	import away3d.materials.ColorMaterial;
	import away3d.primitives.Cube;
	import away3d.primitives.Plane;
	import away3d.primitives.Sphere;
	import away3d.containers.ObjectContainer3D;
	import awayphysics.dynamics.AWPDynamicsWorld;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Vector3D;
	import flash.utils.getTimer;

	public class SceneHolder extends Sprite
	{
		private var _view 				: View3D;		
		private var _light 				: PointLight;
		private var _camController 		: HoverDragController;
		private var _cube 				: Cube;
		private var _sphere 			: Sphere;		
		public static var scene			: Scene3D;
		public static var view			: View3D;
		public static var physicsWorld 	: AWPDynamicsWorld;

		public function SceneHolder()
		{			
			super();
			
			SceneHolder.scene = new Scene3D();
			
			_view = new View3D(SceneHolder.scene);
			_view.antiAlias = 2;
			
			addChild(new AwayStats(_view));
			
			_camController = new HoverDragController(_view.camera, stage);
			_camController.radius = 100;

			addChild(_view);
			addEventListener(Event.ENTER_FRAME, _handleEnterFrame);
			
			_light = new PointLight();
			_light.x = -1000;
			_light.y = 1000;
			_light.z = -1000;
			_light.color = 0xffeeaa;

			_view.scene.addChild(_light);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align 	= StageAlign.TOP_LEFT;
			stage.addEventListener(Event.RESIZE, onStageResize);			
			
			SceneHolder.view = _view;
			
			SceneHolder.physicsWorld = AWPDynamicsWorld.getInstance();
			SceneHolder.physicsWorld.initWithDbvtBroadphase();
			
        }

		private function onStageResize(event : Event) : void
		{
			_view.width = stage.stageWidth;
			_view.height = stage.stageHeight;
		}		
		
		private function _handleEnterFrame(ev : Event) : void
		{
			_view.render();
		}
		
	}
}